package cn.bouncyslime.graduationProject.nba.crawler.pipeline;

import cn.bouncyslime.graduationProject.nba.crawler.spider.NewTeamPageProcessor;
import cn.bouncyslime.graduationProject.nba.dao.CompetitionDao;
import cn.bouncyslime.graduationProject.nba.dto.CompetitionDto;
import cn.bouncyslime.graduationProject.nba.dto.PlayerCompetitionDataDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;

/**
 * @author 张一卓
 * @date 2020/5/6 22:17
 **/
@Component
public class CompetitionPipeline implements Pipeline {

    private static final Logger logger = LoggerFactory.getLogger(CompetitionPipeline.class);

    @Autowired
    CompetitionDao competitionDao;

    @Override
    public void process(ResultItems resultItems, Task task) {
        CompetitionDto competitionDto = resultItems.get("competition");
        List<PlayerCompetitionDataDto> competitionDataDtoList = resultItems.get("playerCompetitionDataList");
        if (competitionDto != null){
            competitionDao.insertCompetition(competitionDto);
        }
        if (competitionDataDtoList != null){
            for (PlayerCompetitionDataDto playerCompetitionDataDto : competitionDataDtoList){
                competitionDao.insertPlayerCompetitionData(playerCompetitionDataDto);
            }
        }
    }
}
